/*
 * @Author: heinan
 * @Date: 2021-07-14 12:21:30
 * @Last Modified by: heinan
 * @Last Modified time: 2021-07-15 11:00:51
 */
import React, { Component } from "react";
import { withInterceptor } from "@/components/interceptor";
import { withRouter } from "react-router";
import { connect } from "react-redux";
import { UPDATE_TITLE } from "@/store/types/home";
import { actionCreator } from "@/store/actions";
import { _getProductList } from "@/store/actions/home";

const mapState2Props = function (store) {
  return store.home;
};
const mapDispatch2props = function (dispatch) {
  return {
    updateTitle(payload) {
      dispatch(actionCreator(UPDATE_TITLE, payload));
    },
    getListByInternet() {
      dispatch(_getProductList(123));
    },
  };
};
// stateProps 当前组件绑定仓库得props对象
// dispatchProps mapDispatch2props中定义得函数
// ownProps 组件得原props
const mergeProps = function (stateProps, dispatchProps, ownProps) {
  return {
    ...ownProps,
    ...dispatchProps,
    ...stateProps,
  };
};
@withInterceptor()
@withRouter
@connect(mapState2Props, mapDispatch2props, mergeProps, {
  // connector 将执行 shouldComponentUpdate 并且浅对比 mergeProps 的结果，避免不必要的更新
  pure: true,
  // connector 会保存一个对被包装组件实例的引用，该引用通过 getWrappedInstance() 方法获得
  withRef: false,
})
class Home extends Component {
  state = {
    usernanme: "heinan",
    password: "123131",
  };
  componentDidMount() {
    //this.props.dispatch({ type: UPDATE_TITLE, payload: "memeda" });
    this.props.updateTitle("memeda");
    this.props.getListByInternet();
    console.log();
  }
  render() {
    const { title } = this.props;
    return <div>this is home{title}</div>;
  }
}
export default Home;
